#!/bin/bash
set -xe

export `cat /etc/os-release | grep UBUNTU_CODENAME`

echo "deb http://mirrors.cloud.tencent.com/ubuntu ${UBUNTU_CODENAME} main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu ${UBUNTU_CODENAME}-updates main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu ${UBUNTU_CODENAME}-backports main restricted universe multiverse
deb http://mirrors.cloud.tencent.com/ubuntu ${UBUNTU_CODENAME}-security main restricted universe multiverse" | sudo tee /etc/apt/sources.list

sudo apt update
sudo apt autoremove -y
sudo apt upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"

# Install OpenSSH-Server
sudo apt install -y openssh-server openssh-sftp-server

echo 'PermitRootLogin prohibit-password
PubkeyAuthentication yes
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM yes
X11Forwarding no
PrintMotd no
AcceptEnv LANG LC_*' | sudo tee /etc/ssh/sshd_config.d/10-ssh-setup.conf

# Install Podman
. /etc/os-release
echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
curl -L "https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/Release.key" | sudo apt-key add -
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y install podman

# Enable rootless container resource limit support
sudo mkdir -p /etc/systemd/system/user@.service.d/
echo '[Service]
Delegate=memory pids cpu io' | sudo tee /etc/systemd/system/user@.service.d/delegate.conf

# Set cgroups v2 as default
eval `cat /etc/default/grub | grep GRUB_CMDLINE_LINUX=` && GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX} systemd.unified_cgroup_hierarchy=1" && sudo -E sed -i "s/^GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"${GRUB_CMDLINE_LINUX}\"/g" /etc/default/grub
sudo update-grub


# Done
set +e
echo 'System restart required. Be sure you have setup authorized_keys before reboot.'
